import 'package:ad_common/third_ad.dart';
import 'package:flutter/foundation.dart';
import 'package:google_ad_plugin/google_ad_manager.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

/// FileName google_reward_video_ad
/// @Author zhuqingfang
/// @Date 2022/9/30 4:19 下午
/// @Description google reward video ad
class GoogleRewardVideoAd extends ThirdAd {
  GoogleRewardVideoAd({required ThirdAdParams thirdAdParams})
      : super(thirdAdParams: thirdAdParams);
  RewardedAd? _rewardedAd;

  @override
  void load() {
    if (!isSdkInit) {
      init();
    }
    _loadRewardVideoAd();
  }

  @override
  void show() {
    _showVideoAd();
  }

  void _loadRewardVideoAd() {
    RewardedAd.load(
        adUnitId: thirdAdParams.placementId,
        request: const AdRequest(),
        rewardedAdLoadCallback: RewardedAdLoadCallback(
          onAdLoaded: (RewardedAd ad) {
            debugPrint('google $ad loaded.');
            _rewardedAd = ad;
            thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adLoaded,
                'google reward video ad placementID:${thirdAdParams.placementId} video loaded');
          },
          onAdFailedToLoad: (LoadAdError error) {
            debugPrint('google RewardedAd failed to load: $error');
            thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adLoadFail,
                'google reward video ad placementID:${thirdAdParams.placementId} video load fail');
          },
        ));
  }

  void _showVideoAd() {
    if (_rewardedAd == null) {
      thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adShowFail,
          'google reward video ad placementID:${thirdAdParams.placementId} video show fail');
      return;
    }
    _rewardedAd?.fullScreenContentCallback = FullScreenContentCallback(
      onAdShowedFullScreenContent: (RewardedAd ad) {
        debugPrint('$ad onAdShowedFullScreenContent.');
        thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adOnShow,
            'google reward video ad placementID:${thirdAdParams.placementId} onShow');
      },
      onAdDismissedFullScreenContent: (RewardedAd ad) {
        debugPrint('$ad onAdDismissedFullScreenContent.');
        thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adOnDismissed,
            'google reward video ad placementID:${thirdAdParams.placementId} onDismiss');
        _rewardedAd = null;
        ad.dispose();
      },
      onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) {
        debugPrint('$ad onAdFailedToShowFullScreenContent: $error');
        thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adShowFail,
            'google reward video ad placementID:${thirdAdParams.placementId} show Fail');
        _rewardedAd = null;
        ad.dispose();
      },
      onAdImpression: (RewardedAd ad) => debugPrint('$ad impression occurred.'),
    );
    _rewardedAd?.show(
        onUserEarnedReward: (AdWithoutView ad, RewardItem reward) {
          thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adOnReward,
              'google reward video ad placementID:${thirdAdParams.placementId} on Reward');
        });
  }
}
